package com.yang.service.impl;

import com.yang.constant.ConfiConstant;
import com.yang.dao.CheckInDao;
import com.yang.dao.CheckOutDao;
import com.yang.dao.LoginDao;
import com.yang.dao.SelectInformationDao;
import com.yang.entity.*;
import com.yang.pojo.UncheckedDto;
import com.yang.service.CheckOutService;
import org.apache.commons.lang3.StringUtils;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CheckOutSerciceImpl implements CheckOutService {
    private static Logger logger = Logger.getLogger(CheckOutSerciceImpl.class);

    @Resource
    CheckInDao checkInDao;

    @Resource
    LoginDao loginDao;

    @Resource
    CheckOutDao checkOutDao;

    @Resource
    SelectInformationDao selectInformationDao;

    @Override
    public Map<String, Object> dectection(String roomNun, int day) {
        Map<String,Object> map = new HashMap<String, Object>();
        if(day <= 0){
            map.put("status","error");
            map.put("info","入天数不能小于0");
            logger.error("入住天数小于0");
            return map;
        }
        RoomEntity roomEntity = null;
        roomEntity = selectInformationDao.selectRoomByRoomNum(roomNun);
        if(roomEntity == null){
            map.put("status","error");
            map.put("info","房号出错");
            logger.error("在数据库没有找到房间号");
            return map;
        }
        if(roomEntity.getStatus().equals("0")){
            map.put("status","error");
            map.put("info","请确认是否为续租客人！");
            logger.error("房间还未被租用");
            return map;
        }
        RateEntity rateEntity = null;
        rateEntity = checkInDao.selectRateById(roomEntity.getMoneyId());

        if(rateEntity == null ){
            map.put("status","error");
            map.put("info","网络出错");
            logger.error("在数据库没有找到");
            return map;
        }
        logger.info("RateEntity = "+rateEntity.toString());
        float money = rateEntity.getMoney()*day;
        map.put("status","succeed");
        map.put("info",money);
        return map;
    }

    @Override
    public Map<String, Object> relet(String roomNum, int day, float money) {
        Map<String,Object> map = new HashMap<String, Object>();
        //判断钱不可以少于0
        if(money<0){
            map.put("status","error");
            map.put("info","付款错误");
            logger.error("money小于0");
            return map;
        }
        //判断入住天数不可以小于0
        if(day <= 0){
            map.put("status","error");
            map.put("info","入天数不能小于0");
            logger.error("入住天数小于0");
            return map;
        }
        //获得房间实体
        RoomEntity roomEntity = null;
        logger.info("5");
        roomEntity = selectInformationDao.selectRoomByRoomNum(roomNum);
        if(roomEntity == null){
            map.put("status","error");
            map.put("info","房号出错");
            logger.error("在数据库没有找到房间号");
            return map;
        }
        if(roomEntity.getStatus().equals("0")){
            map.put("status","error");
            map.put("info","请确认是否为续租客人！");
            logger.error("房间还未被租用");
            return map;
        }

        //获得关系表实体
        logger.info("4");
        LiverRelationEntity liverRelationEntity = checkOutDao.selectLiveByRoomIdAndStatus(roomEntity.getId(),"1");
        if(liverRelationEntity == null){
            map.put("status","error");
            map.put("info","网络出错");
            logger.error("liverRelationEntity找不到"+roomEntity.toString());
            return map;
        }
        //从关系表中获得order表
        logger.info("3");
        OrderFormEntity orderFormEntity = selectInformationDao.selectOrderByOrderId(liverRelationEntity.getOrderFormId());
        if(orderFormEntity == null){
            map.put("status","error");
            map.put("info","网络出错");
            logger.error("liverRelationEntity找不到"+liverRelationEntity.toString());
            return map;
        }

        //获得rate表
        RateEntity rateEntity = null;
        logger.info("2");
        rateEntity = checkInDao.selectRateById(roomEntity.getMoneyId());

        if(rateEntity == null ){
            map.put("status","error");
            map.put("info","网络出错");
            logger.error("在数据库没有找到");
            return map;
        }
        //计算费用
        money = orderFormEntity.getPay()+money;
        float rantal = orderFormEntity.getRantal() + rateEntity.getMoney()*day;
        day = orderFormEntity.getStayDay()+day;
        orderFormEntity.setPay(money);
        orderFormEntity.setRantal(rantal);
        orderFormEntity.setStayDay(day);
        logger.info("1");
        if(!checkOutDao.updataOrder(orderFormEntity)){
            map.put("status","error");
            map.put("info","网络出错");
            logger.error("order表更新失败"+orderFormEntity);
            return map;
        }
        map.put("status","succeed");
        map.put("info","续租成功！");
        logger.info("更新order表成功！");
        return map;
    }

    @Override
    public Map<String, Object> settleAccounts(String roomNum) {
        Map<String,Object> map = new HashMap<String, Object>();
        RoomEntity roomEntity = null;
        roomEntity = selectInformationDao.selectRoomByRoomNum(roomNum);
        if(roomEntity == null){
            map.put("status","error");
            map.put("info","房号出错");
            logger.error("在数据库没有找到房间号");
            return map;
        }
        if(roomEntity.getStatus().equals("0")){
            map.put("status","error");
            map.put("info","该客房还未被使用！");
            logger.error("房间还未被租用");
            return map;
        }
        RateEntity rateEntity = checkInDao.selectRateById(roomEntity.getMoneyId());
        //获得关系表实体
        LiverRelationEntity liverRelationEntity = checkOutDao.selectLiveByRoomIdAndStatus(roomEntity.getId(),"1");
        if(liverRelationEntity == null){
            map.put("status","error");
            map.put("info","网络出错");
            logger.error("liverRelationEntity找不到"+roomEntity.toString());
            return map;
        }
        OrderFormEntity orderFormEntity = selectInformationDao.selectOrderByOrderId(liverRelationEntity.getOrderFormId());
        if(orderFormEntity == null){
            map.put("status","error");
            map.put("info","网络出错");
            logger.error("liverRelationEntity找不到"+liverRelationEntity.toString());
            return map;
        }

        long beginDay = orderFormEntity.getBeginDay();
        long oneDay = 86400000L;
        long today = System.currentTimeMillis();
        long difference = today - beginDay;
        int day = 0;
        if(difference>0){
            day = (int) (difference / oneDay);
            if(day==0){
                day++;
            }
        }
        logger.info("beginDay = "+beginDay+" today = "+today + " day = "+day+" difference = "+difference);

        float money = rateEntity.getMoney() * day;
        float needReturn = orderFormEntity.getPay() - money;

        logger.info("money = "+money + " needRetrun = "+needReturn);

        roomEntity.setStatus("0");
        checkInDao.updataRoom(roomEntity);
        orderFormEntity.setPay(money);
        orderFormEntity.setRantal(money);
        orderFormEntity.setStayDay(day);
        checkOutDao.updataOrder(orderFormEntity);
        logger.info(orderFormEntity.toString());

        List<LiverRelationEntity> liverRelationEntities = checkOutDao.selectLiverListByOrderId(orderFormEntity.getId());
        if(liverRelationEntities == null){
            logger.error("licer == null");
        }
        for (LiverRelationEntity liverRelationEntity1 : liverRelationEntities){
            liverRelationEntity1.setStatus("0");
            checkOutDao.updataLiver(liverRelationEntity1);
        }
        map.put("status","succeed");
        map.put("info",needReturn);
        return map;
    }

    @Override
    public Map<String, Object> selectUnchecked(String roomNum, String name) {
        Map<String,Object> map = new HashMap<String, Object>();
        List<RelationEntity> relationEntities = null;
        if(!StringUtils.isBlank(roomNum)){
            relationEntities = checkOutDao.selectRelationByNameAndguestStatus(roomNum, ConfiConstant.HOMEOWNER);
        }else if (!StringUtils.isBlank(name)){
            relationEntities = checkOutDao.selectRelationByNameAndguestStatus(name, ConfiConstant.HOMEOWNER);
        }else{
            map.put("status","error");
            map.put("info","请输入搜索值");
            logger.error("input null");
            return map;
        }
        List<UncheckedDto> uncheckedDtos = new ArrayList<UncheckedDto>();
        for(RelationEntity relationEntity :relationEntities){
           OrderFormEntity orderFormEntity = relationEntity.getOrderFormId();
           long today = System.currentTimeMillis();
           long liveDay = orderFormEntity.getBeginDay()+orderFormEntity.getStayDay()*ConfiConstant.DAY;
           if(today<liveDay){
               continue;
           }
           if(orderFormEntity.getRantal() == orderFormEntity.getPay()){
               continue;
           }
           float money = orderFormEntity.getRantal() - orderFormEntity.getRantal();
            UncheckedDto uncheckedDto = new UncheckedDto(orderFormEntity.getId(),relationEntity.getRoomId().getRoomNum(),
                    relationEntity.getGuestId().getName(),relationEntity.getGuestId().getIdetifcation(),
                    relationEntity.getGuestId().getTel(),money);
            uncheckedDtos.add(uncheckedDto);
        }
        map.put("status","succeed");
        map.put("info",uncheckedDtos);
        return map;
    }

    @Override
    public Map<String, Object> finishUnchecked(long orderId) {
        Map<String,Object> map = new HashMap<String, Object>();
        OrderFormEntity orderFormEntity = checkOutDao.selectOrderByOrderId(orderId);
        if(orderFormEntity == null){
            map.put("status","error");
            map.put("info","结账编号错误");
            logger.error("通过ID找不到记录");
            return map;
        }
        long today = System.currentTimeMillis();
        long liveDay = orderFormEntity.getBeginDay()+orderFormEntity.getStayDay()*ConfiConstant.DAY;
        if(today<liveDay){
            map.put("status","error");
            map.put("info","结账编号错误");
            logger.error("还未到计算时间");
            return map;
        }
        if(orderFormEntity.getRantal() == orderFormEntity.getPay()){
            map.put("status","error");
            map.put("info","结账编号错误");
            logger.error("不需要结算");
            return map;
        }
        float money = orderFormEntity.getRantal() - orderFormEntity.getRantal();
        orderFormEntity.setPay(orderFormEntity.getRantal());
        checkOutDao.updataOrder(orderFormEntity);
        List<LiverRelationEntity> liverRelationEntities = checkOutDao.selectLiverListByOrderId(orderId);
        if(!liverRelationEntities.isEmpty()){
            for(LiverRelationEntity liverRelationEntity : liverRelationEntities){
                liverRelationEntity.setStatus(ConfiConstant.HISTORICAL_RELATIONSHIP);
                checkOutDao.updataLiver(liverRelationEntity);
            }
        }
        map.put("status","succeed");
        map.put("info",money);
        return map;
    }

}
